package com.aohe.youzi.base;

import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;
import androidx.viewbinding.ViewBinding;


import com.aohe.youzi.databinding.ActivityBaseBinding;
import com.aohe.youzi.databinding.TitlebarBinding;
import com.aohe.youzi.utils.EditTextUtil;
import com.aohe.youzi.utils.Utils;

import flyn.ScreenUtils;
import flyn.StatusBarUtil;

public abstract class BaseActivity<VB extends ViewBinding> extends FragmentActivity {

    private long timeMillis;
    public Activity activity;
    public Long startTimeMillis;
    public boolean isFinish = true;
    ActivityBaseBinding baseBinding;
    TitlebarBinding titlebarBinding;
    public int width, height, soundid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        this.getWindow().setFlags(0x80000000, 0x80000000);
        baseBinding = ActivityBaseBinding.inflate(getLayoutInflater());
        titlebarBinding = TitlebarBinding.bind(baseBinding.baseTop.getRoot());

        View rootView = baseBinding.getRoot();
        setContentView(rootView);
        baseBinding.framelayout.addView(getViewBinding().getRoot());

        ScreenUtils.setStatusBarTransparentAndWordsGray(getWindow());
        StatusBarUtil.setStatusBarLightMode(this, Color.WHITE);
//        ScreenUtils.setStatusBarTransparent(getWindow());
        activity = this;
        titlebarBinding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isFinish) {
                    finish();
                } else {
                    setBack();
                }
            }
        });
        titlebarBinding.tvTitle.setText(setTitle());
        Display display = getWindowManager().getDefaultDisplay();
        width = display.getWidth();
        height = display.getHeight();
        startTimeMillis = System.currentTimeMillis();
        initData();
    }

    /**
     * bind layout resource file
     */
    protected abstract VB getViewBinding();

    protected abstract String setTitle();

    protected abstract void initData();

    public void setBack() {
    }

    public void setTitle(String title) {
        titlebarBinding.tvTitle.setText(title);
    }

    public void setRight(String title) {
        titlebarBinding.tvRight.setText(title);
        titlebarBinding.tvRight.setVisibility(View.VISIBLE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        //根据 Tag 取消请求
//        OkGo.getInstance().cancelTag(this);
//
//        //取消所有请求
//        OkGo.getInstance().cancelAll();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //获取当前获得焦点的View
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            //调用方法判断是否需要隐藏键盘
            EditTextUtil.hideInput(activity);
        }
        return super.dispatchTouchEvent(ev);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


}
